package com.copsoftware.reportes.geografico.support;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.TreeMap;

import com.copsoftware.reportes.geografico.beans.BeanJSONString;
import com.copsoftware.reportes.geografico.beans.BeanOcurrencia;
import com.copsoftware.reportes.geografico.beans.BeanTematicoZonal;
import com.copsoftware.support.json.JSONObject;

public class BeanPopulateTematicoZonal {

	public static List procesar(int numIntervalos, int numMaxDelito) {
		int firstRange = (numMaxDelito / numIntervalos) ;
		int nextRange = firstRange;
		List listBeanOcurrencia = new ArrayList();
		for (int i = 0; i < numIntervalos; i++) {			
//			BeanOcurrencia bn = new BeanOcurrencia();
			BeanTematicoZonal bn = new BeanTematicoZonal();			
			if (i == 0) {
				bn.setRangoMin("0");
				if (numIntervalos == 1)
					bn.setRangoMax(String.valueOf(numMaxDelito));
				else {
					bn.setRangoMax(String.valueOf(firstRange));
				}
			} else {
				bn.setRangoMin(String.valueOf(nextRange));
				if (i + 1 == numIntervalos) {					
					bn.setRangoMax(String.valueOf(numMaxDelito));
				}
				else {
					int curretMaxRange = nextRange+firstRange;
					bn.setRangoMax(String.valueOf(curretMaxRange));
					nextRange=curretMaxRange;
				}
			}
			nextRange += 1;
			listBeanOcurrencia.add(bn);
		}
		return listBeanOcurrencia;
	}

	public static List crearJSONObjects(Collection listaBeanTematicoZonal) {
		List listaJSONStringObjects = new ArrayList();
		for (Iterator iter = listaBeanTematicoZonal.iterator(); iter.hasNext();) {
			BeanTematicoZonal element = getBeanTematicoZonal(iter);			
//			JSONObject jsonObject = new JSONObject(element);
//			BeanJSONString bnBeanJSONString = new BeanJSONString(jsonObject.toString());
			BeanJSONString bnBeanJSONString = BeanJSONStringBuilder.execute(element);			
			listaJSONStringObjects.add(bnBeanJSONString);
		}
		return listaJSONStringObjects;
	}

	private static BeanTematicoZonal getBeanTematicoZonal(Iterator iter) {
		BeanTematicoZonal element = (BeanTematicoZonal) iter.next();
		String [] rangos = element.getXRango().split("-");
		element.setRangoMin(rangos[0]);
		element.setRangoMax(rangos[1]);
		return element;
	}
}
